home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / Float.st < prev    next >
Text File  |  1991-09-12  |  5KB  |  217 lines

  1. "======================================================================
  2. |
  3. |   Float Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         12 Sep 91      Fixed storeOn: to not be recursive
  34. |
  35. | sbyrne     15 Apr 90      Added asFloat...I could have sworn this was already
  36. |                         here...must have been lost in an edit.
  37. |
  38. | sbyrne     25 Apr 89      created.
  39. |
  40. "
  41.  
  42.  
  43.  
  44. Number variableWordSubclass: #Float "not really a variable class"
  45.        instanceVariableNames: ''
  46.        classVariableNames: ''
  47.        poolDictionaries: ''
  48.        category: nil.
  49.  
  50. Float comment: 
  51. 'My instances represent floating point numbers that have 64 bits of 
  52. precision (well, less than that in precision; they are precisely the same
  53. as C''s "double" datatype).  Besides the standard numerical operations,
  54. I provide transcendental operations too.'  !
  55.  
  56. !Float class methodsFor: 'basic'!
  57.  
  58. pi
  59.     "Returns the value of pi"
  60.     ^3.14159265358979323846264338327950288
  61. !!
  62.  
  63.  
  64.  
  65. !Float methodsFor: 'arithmetic'!
  66.  
  67. // aNumber
  68.     "Return the integer quotient of dividing the receiver by aNumber with
  69.     truncation towards negative infinity."
  70.     ^(self / aNumber) floor
  71. !
  72.  
  73. \\ aNumber
  74.     "Return the integer remainder of dividing the receiver by aNumber with
  75.     truncation towards negative infinity."
  76.     ^(self - ((self / aNumber) integerPart * aNumber)) floor
  77. !
  78.  
  79. integerPart
  80.     ^self - self fractionPart
  81. !!
  82.  
  83.  
  84.  
  85. !Float methodsFor: 'coercing methods'!
  86.  
  87. coerce: aNumber
  88.     ^aNumber asFloat
  89. !
  90.  
  91. generality
  92.     ^4
  93. !
  94.  
  95. asFloat
  96.     "Just defined for completeness."
  97.     ^self
  98. !
  99.  
  100. hash
  101.     "Returns the hash value for the receiver (a Float).  If it's representable
  102.     as an integer, we use that value, since it's likely to be much more random,
  103.     but if not, we use the exponent.  Another approach that may be used in 
  104.     the future is to scale the number so that it's within the integer range if
  105.     it's not normally and then return the integer part"
  106.     | expt |
  107.     expt _ self exponent.
  108.     expt >= 0 & (expt < 31)
  109.         ifTrue: [ ^self truncated ]
  110.     ifFalse: [ ^expt ]
  111. !!
  112.  
  113.  
  114.  
  115. !Float methodsFor: 'copying'!
  116.  
  117. shallowCopy
  118.     ^self
  119. !
  120.  
  121. deepCopy
  122.     ^self
  123. !!
  124.  
  125.  
  126.  
  127.  
  128. !Float methodsFor: 'printing'!
  129.  
  130. printOn: aStream
  131.     | num exp |
  132.     (self exponent between: -51 and: 51)
  133.         ifTrue: [ self printFloatOn: aStream ]
  134.     ifFalse: [ exp _ 0.
  135.                num _ self.
  136.            num abs < 1.0
  137.               ifTrue: [ [ num abs < 1.0 ] whileTrue:
  138.                     [ num _ num * 10.0.
  139.                       exp _ exp - 1 ] ]
  140.               ifFalse: [ [num abs >= 10.0] whileTrue:
  141.                               [ num _ num / 10.0.
  142.                       exp _ exp + 1 ] ].
  143.                    num printFloatOn: aStream.
  144.            aStream nextPut: $e.
  145.            exp printOn: aStream ].
  146. !!
  147.  
  148.  
  149.  
  150. !Float methodsFor: 'storing'!
  151.  
  152. storeOn: aStream
  153.     self printOn: aStream
  154. !!
  155.  
  156.  
  157.  
  158. !Float methodsFor: 'private'!
  159.  
  160. printFloatOn: aStream
  161.     | num str |
  162.     num _ self.
  163.     num < 0 ifTrue:
  164.         [ aStream nextPut: $-.
  165.       num _ num negated. ].
  166.     num printIntegerPartOn: aStream.
  167.     aStream nextPut: $..
  168.     num printFracPartOn: aStream.
  169. !
  170.  
  171. printIntegerPartOn: aStream
  172.     | num |
  173.     num _ self integerPart.
  174.     (num revDigitsBase: 10.0) reverseDo:
  175.         [ :char | aStream nextPut: char ]
  176. !
  177.  
  178. printFracPartOn: aStream
  179.     | num count zeroCount digit |
  180.     num _ self fractionPart.
  181.     num = 0.0 ifTrue: [ ^aStream nextPut: $0 ].
  182.     zeroCount _ count _ 0.
  183.  
  184.     " produce the digits, up to a maximum of 15, suppressing trailing
  185.       zeros "
  186.     [ (num ~= 0.0) & (count < 15) ] whileTrue:
  187.         [ num _ num * 10.0.
  188.       digit _ num floor.
  189.       digit = 0        "for trailing zero suppression"
  190.         ifTrue: [ zeroCount _ zeroCount + 1 ]
  191.         ifFalse: [ aStream next: zeroCount put: $0.
  192.                        aStream nextPut: (Character digitValue: digit).
  193.                    zeroCount _ 0 ].
  194.       num _ num fractionPart.
  195.       count _ count + 1 ].
  196.  
  197.     zeroCount = 15 ifTrue: [ aStream nextPut: $0 ]
  198. !
  199.  
  200. "AARRGGHH!  Why can't I share this code with integer?  Grrr...."
  201. revDigitsBase: b    
  202.     | str num |
  203.     str _ WriteStream on: (String new: 1).
  204.     self = 0
  205.         ifTrue: [ str nextPut: $0 ]
  206.     ifFalse: [
  207.             num _ self.
  208.         [ num = 0 ] whileFalse:
  209.             [ str nextPut: (Character digitValue: num \\ b).
  210.           num _ (num / b) integerPart ] ].
  211.     ^str contents
  212.  
  213. !!
  214.  
  215.